BaseGenerator.js ➔ ???   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 2
c 3
b 0
f 3
nc 1
dl 0
loc 5
rs 9.4285
nop 0
1
const fs   = require('fs'),
2
      _    = require('lodash'),
3
      yaml = require('yaml')
4
5
/**
6
 * @abstract @class BaseGenerator
7
 *
8
 * Base generator class
9
 */
10
class BaseGenerator {
11
  /**
12
   * Base generator constructor
13
   */
14
  constructor () {
15
    if (new.target === BaseGenerator) {
16
      throw new TypeError('BaseGenerator is abstract class')
17
    }
18
  }
19
20
  /**
21
   * Check swagger file exists
22
   *
23
   * @param {boolean|undefined} checked First checked is not checked, add relative path
24
   * @param {String|undefined} propertyName Property name
25
   * @param {String|undefined} message Throw error message
26
   *
27
   * @throws Error if file not found
28
   *
29
   * @private
30
   */
31
  _checkFile (checked, propertyName, message) {
32
    message = message || 'Swagger json or yml file does not exists'
33
    propertyName = propertyName || 'filename'
34
    if (fs.existsSync(this[propertyName])) {
35
      return true
36
    }
37
38
    let filename = (process.cwd() + '/' + this[propertyName]).replace(/\/\//g, '/')
39
40
    if (!fs.existsSync(this[propertyName]) && (filename !== this[propertyName]) && checked !== true) {
41
      this[propertyName] = filename
42
      this._checkFile(true, propertyName, message)
43
    }
44
45
    throw new Error(message)
46
  }
47
48
  /**
49
   * Parse data from file
50
   *
51
   * @return {Swagger20}
52
   * @private
53
   */
54
  _parseData (content) {
55
    if (_.isString(content)) {
56
      this._content = content
57
    }
58
59
    let data = undefined
0 ignored issues
show
Unused Code Comprehensibility introduced by
The assignment of undefined is not necessary as data is implicitly marked as undefined by the declaration.
Loading history...
60
    try {
61
      data = JSON.parse(this._content)
62
    } catch (e) {
63
      try {
64
        data = yaml.tokenize(this._content)
65
      } catch (e) {
66
        throw new Error('Error parse data from file')
67
      }
68
    }
69
70
    return data
71
  }
72
}
73
74
/**
75
 * @ignore module
76
 * @ignore module.exports
77
 */
78
module.exports = BaseGenerator